/**
 * 
 */
package ru.athena.runTool.Model;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import ru.athena.runTool.LogWriter;
import ru.athena.runTool.blockTypes.CodeBlock;

/**
 * @author corc_usr
 *
 */
public class HierarchicalVisitorFiltered implements HierarchicalVisitor {
	private List<Filter> 	filters_ = new ArrayList<Filter>();		// one or more filter conditions
	private List<Operation> operators_ = new ArrayList<Operation>();	// one or more operators	
	
	
	/** Add a filter
	 * 
	 * @param filter - Filter object
	 */
	public void add( Filter filter ) {
		filters_.add( filter );
	}


	/** Add an operator
	 * 
	 * @param process - Operation object (functor/visitor) 
	 */
	public void add( Operation process ) {
		operators_.add( process );
	}
	

	/**
	 * Empty. Nothing to do with composite nodes itself
	 */
	public boolean visitEnter(CompositeNode node)  throws Exception {
		// Return first that rejects this node
		for (Filter f : filters_) {
			if ( !f.canVisit(node)) {
				return false;
			}
		}
		for (Operation op : operators_) {
			op.visitEnter( node );
		}
		LogWriter.out().println( "=> [" + ((CodeBlock)node.getUserObject()).getText() + "]" );
		return true;
	}

	/** 
	 * Empty. Nothing to do with composite nodes itself
	 * @throws Exception 
	 */
	public boolean visitLeave(CompositeNode node) throws Exception {
		for (Operation op : operators_) {
			op.visitLeave( node );
		}
		LogWriter.out().println( "<= [" + ((CodeBlock)node.getUserObject()).getText() + "]" );		
		return true;
	}

	/** 
	 * execute this node
	 * @throws Exception 
	 */
	public boolean visit(LeafNode node) throws Exception {
		// Return first that rejects this node
		for (Filter f : filters_) {
			if ( !f.canVisit(node)) {
				return false;
			}
		}
		LogWriter.out().println( ">" + ((CodeBlock)node.getUserObject()).getText() );		
		for (Operation op : operators_) {
			op.visit( node );
		}
		return true;
		
	}
	
}
